package edu.scau.cxq.gp.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import edu.scau.cxq.gp.common.annotation.ResponseResult;
import edu.scau.cxq.gp.common.utils.WrapperUtils;
import edu.scau.cxq.gp.entity.dto.WarehouseSignDTO;
import edu.scau.cxq.gp.entity.Warehouse;
import edu.scau.cxq.gp.service.IWarehouseService;
import edu.scau.cxq.gp.entity.vo.WarehouseVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * @description 存储仓库信息 前端控制器
 * @return
 * @author TLC
 */

@RestController
@ResponseResult
@RequestMapping("/warehouse")
public class WarehouseController {

    @Resource
    private IWarehouseService warehouseService;

    /**
     * 插入新仓库
     * @param warehouse
     * @return
     */
    @PostMapping("/insertOne")
    public boolean insertOne(@RequestBody Warehouse warehouse) {

        return warehouseService.save(warehouse);
    }

    /**
     * 查找仓库
     * @return
     */
    @GetMapping("/select")
    public List<Warehouse> select(Warehouse warehouse) {
        QueryWrapper queryWrapper = WrapperUtils.entitytoWrapper(warehouse);
        queryWrapper.eq("ware_hidden", 0);
        List<Warehouse> warehouses = warehouseService.list(queryWrapper);
        return warehouses;
    }

    /**
     * 查找单个仓库
     */
    @GetMapping("/selectOne")
    public Warehouse selectOne (Long wareId) {
        QueryWrapper<Warehouse> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("ware_id", wareId);
        Warehouse warehouse = warehouseService.getOne(queryWrapper);

        return warehouse;
    }

    /**
     * 分页查找仓库
     * @return
     */
    @GetMapping("/selectByPage")
    public IPage<Warehouse> selectByPage (Integer currentPage, Integer size, Warehouse warehouse) {
        Page<Warehouse> page = new Page<>(currentPage, size);
        QueryWrapper queryWrapper = WrapperUtils.entitytoWrapper(warehouse);
        queryWrapper.eq("ware_hidden", 0);
        return warehouseService.page(page, queryWrapper);
    }

    /**
     * 删除（实际上是隐藏）
     * @param warehouse
     * @return
     */
    @PostMapping("/deleteOne")
    public boolean deleteOne(@RequestBody Warehouse warehouse) {
        QueryWrapper queryWrapper = WrapperUtils.entitytoWrapper(warehouse);
        Warehouse ware = warehouseService.getOne(queryWrapper);
        // 将隐藏位置1
        ware.setWareHidden(1);
        return warehouseService.updateById(ware);
    }

    /**
     * 修改仓库
     * @RequestBody：
     * 作用：
     * 主要用来接收前端传递给后端的json字符串中的数据的(请求体中的数据的)；
     * @param warehouse
     * @return
     */
    @PostMapping("/updateOne")
    public boolean updateOne (@RequestBody Warehouse warehouse) {
        return warehouseService.updateById(warehouse);
    }

    /**
     * 获取所有的仓库标签
     * @return
     */
    @GetMapping("/selectWareSigns")
    public List<WarehouseSignDTO> selectWareSigns () {
        return warehouseService.findWareSigns();
    }


    /**
     * 获取仓库的存储信息
     */
    @GetMapping("/getWarehouseVO")
    public WarehouseVO getWarehouseVO(){
        return warehouseService.getWarehouseInfo();
    }
}
